//
//  DownloadEntity.m
//  AJBracelet
//
//  Created by zorro on 15/7/22.
//  Copyright (c) 2015年 zorro. All rights reserved.
//

#import "DownloadEntity.h"
#import "XYSandbox.h"
#import "FileModelEntity.h"

@implementation DownloadEntity

DEF_SINGLETON(DownloadEntity)

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.downloadEngine = [DownloadHelper defaultSettings];
        self.downloadEngine.freezable = NO;
        self.downloadEngine.forceReload = NO;
        
        self.pathArray = @[@"", [XYSandbox libCachePath]];
    }
    
    return self;
}

- (void)downloadFileWithWebsite:(NSString *)website withRequestType:(DownloadEntityRequest)requestType
{
    if (website && website.length > 6) {
        // SHOWMBProgressHUDIndeterminate(@"下载升级程序", nil, NO);
        [self removeLastUpdateWarePatchFile];
        [self.downloadEngine cancelDownloadWithString:website];

        NSString *fileName = [[website componentsSeparatedByString:@"/"] lastObject];
        _filePath = [NSString stringWithFormat:@"%@/%@", [XYSandbox libCachePath], fileName];
        Downloader *down = [self.downloadEngine downLoad:website to:_filePath params:nil breakpointResume:NO];
        NSLog(@"....down = %@", down);
        [down succeed:^(MKNetworkOperation *op) {
            // [self unzipWithZipPath:filePath withRequest:requestType];
            NSLog(@"下载bin成功... %@",_filePath);
            KK_ShareData.isDown = YES;
            if (requestType == DownloadEntityRequestFirmware) {
                [FileModelEntity sharedInstance].wareInfo.isDownload = YES;
//                [AJ_LastWareVersion setIntValue:[FileModelEntity sharedInstance].wareInfo.version.intValue];
            }
        } failed:^(MKNetworkOperation *op, NSError *err) {
        }];
        
        [self.downloadEngine submit:down];
    }
}

@end
